package tech.mobi.child.service;

import java.io.File;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

import org.mymmsc.android.app.util.AndroidXmlParser;
import org.mymmsc.android.app.util.XmlResult;
import org.mymmsc.api.http.HttpClient;
import org.mymmsc.api.network_io.DataStream;

import android.util.Log;

import tech.client.utils.FileUtil;
import tech.client.utils.FormFile;
import tech.client.utils.SocketHttpRequester;
import tech.mobi.child.bean.UserInfoBean;
import tech.mobi.child.utils.Configuration;
import tech.mobi.child.utils.Tools;

public class LoginService {

	public static Object login(String uid, String pwd, String isFlag, String strVersion) {

//		String domain = Configuration.URL + Configuration.PROJECTNAME
//				+ "/servlet/VirtualLoginServlet";
//		XmlResultNew ret = new XmlResultNew();
//		AndroidXmlParser xp = null;
//		String strXML = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n";
//		strXML += "<mobile><request>";
//		strXML += "<action>login</action>";
//		strXML += "<username><![CDATA[" + uid.trim() + "]]></username>";
//		strXML += "<password><![CDATA[" + pwd.trim() + "]]></password>";
//		strXML += "<usertype><![CDATA[" + isFlag.trim() + "]]></usertype>";
//		strXML += "</request></mobile>";
//		String msg = "";
//
//		msg = HttpClient.PostXml(domain, strXML, Configuration.CLIENTINFO,
//				"utf-8", Configuration.EXP_TIMEOUT);
//
//		if (msg != null) {
//			xp = new AndroidXmlParser(msg);
//			try {
//				ret = xp.valueOf("response", XmlResultNew.class);
//			} catch (Exception e) {
//				e.printStackTrace();
//			}
//		}
//
//		return ret;
		String s = Tools.getConstParamentHeader() 
		+ "<username>"+ uid + "</username>" 
		+ "<password>"+ pwd + "</password>" 
		+ "<usertype>"+ isFlag + "</usertype>"
		+ Tools.getConstParamentFooter(strVersion);
		Log.i("MyLog",s);
		
		UserInfoBean ret = null;
		ret = new UserInfoBean();
		ret.setStatus("000000");
		ret.setMessage("登录成功！");
		ret.setUserid("0001");
		ret.setUsername("JS001");
		ret.setPassword("123456");
		ret.setTurename("杨老师");
		ret.setUsersex("男");
		ret.setHeadpicture("");
		ret.setUsertype("教师");
		return ret;
		
	}

	/**
	 * 实现客户信息保存 2012-10-12 ylj
	 * 
	 * @param phoneId
	 *            手机号码
	 * @param passwordId
	 *            密码
	 * @param passwordDId
	 *            重复密码
	 * @param userName
	 *            用户名
	 * @param sex
	 *            性别
	 * @param imgFile
	 *            图片文件
	 * @return
	 */
	public static Object saveCustomMessage(String phoneId, String passwordId,
			String passwordDId, String userName, String sex, String imgFile) {
		if (imgFile.equals("")) {
			String path = Configuration.URL + Configuration.PROJECTNAME
					+ "/servlet/Portal";
			XmlResult ret = null;
			AndroidXmlParser xp = null;
			String strXML = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n";
			strXML += "<mobile><request>";
			strXML += "<method>register-Userregeditnopicture</method>";
			strXML += "<phonenum>" + phoneId + "</phonenum>";
			strXML += "<password>" + passwordId + "</password>";
			strXML += "<repassword>" + passwordDId.trim() + "</repassword>";
			strXML += "<nickname><![CDATA[" + userName.trim()
					+ "]]></nickname>";
			strXML += "<sex>" + sex.trim() + "</sex>";
			strXML += "</request></mobile>";

			String msg = "";
			msg = HttpClient.PostXml(path, strXML, Configuration.CLIENTINFO,
					"utf-8", Configuration.EXP_TIMEOUT);
			if (msg != null) {
				xp = new AndroidXmlParser(msg);
				try {
					ret = (XmlResult) xp.valueOf("response", XmlResult.class);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			if (ret == null) {
				ret = new XmlResult();
			}
			return ret;
		} else {

			boolean b = false;
			File cachePhoto = new File(imgFile);
			if (cachePhoto.exists()) {
				String path = Configuration.URL + Configuration.PROJECTNAME
						+ "/servlet/UserRegisterServlet";
				Map<String, String> params = new HashMap<String, String>();
				params.put("phonenum", phoneId);
				params.put("password", passwordId);
				params.put("repassword", passwordDId);
				params.put("nickname", userName);
				params.put("sex", sex);
				String fileType = FileUtil.getFileType(cachePhoto);
				params.put("filename", cachePhoto.getName());
				String postfix = cachePhoto.getName().substring(
						cachePhoto.getName().lastIndexOf("."));
				params.put("postfix", postfix);
				FormFile formFile = new FormFile(cachePhoto, "upfile", fileType);
				try {
					// todo .....
					b = SocketHttpRequester.post(path, params, formFile, 80);
				} catch (Exception e) {
					e.printStackTrace();
				}

			}

			XmlResult ret = new XmlResult();
			if (b) {
				ret.status = Configuration.EXP_SUCCESS;
				ret.message = "恭喜您，注册成功！";
			} else {
				ret.status = "111111";
				ret.message = "注册失败！";
			}
			return ret;
		}

	}

	/**
	 * 实现会员认证信息的保存 2012-10-12 ylj
	 * 
	 * @param phoneNumber
	 * @param trueName
	 * @param idCard
	 * @return
	 */
	public static Object saveCustomVerMessage(String phoneNumber,
			String trueName, String idCard, String empId) {
		String domain = Configuration.URL + Configuration.PROJECTNAME
				+ "/servlet/Portal";
		XmlResult ret = null;
		String strXML = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n";
		strXML += "<mobile><request>";
		strXML += "<method>register-usercertify</method>";
		strXML += "<phonenum>" + phoneNumber.trim() + "</phonenum>";
		strXML += "<username><![CDATA[" + trueName.trim() + "]]></username>";
		// strXML += "<username>" + trueName.trim() + "</username>";
		strXML += "<identitycard><![CDATA[" + idCard.trim()
				+ "]]></identitycard>";
		strXML += "<empid>" + empId + "</empid>";
		strXML += "</request></mobile>";
		AndroidXmlParser xp = null;
		String msg = "";
		msg = HttpClient.PostXml(domain, strXML, Configuration.CLIENTINFO,
				"utf-8", Configuration.EXP_TIMEOUT);
		if (msg != null) {
			xp = new AndroidXmlParser(msg);
			try {
				ret = xp.valueOf("response", XmlResult.class);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return ret;
	}

	public static Object addMIEI(String mieiStr, String deviceType,
			String systemID, String phoneType, String phoneNumber,
			String versionNumber) {
		String domain = Configuration.URL + Configuration.PROJECTNAME
				+ "/servlet/Portal";
		XmlResult ret = null;
		String strXML = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n";
		strXML += "<mobile><request>";
		strXML += "<method>manage-Devicemiei</method>";
		strXML += "<devicemiei>" + mieiStr.trim() + "</devicemiei>";
		strXML += "<devicetype>" + deviceType.trim() + "</devicetype>";
		strXML += "<systemid>" + systemID.trim() + "</systemid>";
		strXML += "<phonetype>" + phoneType.trim() + "</phonetype>";
		strXML += "<phonenumber>" + phoneNumber.trim() + "</phonenumber>";
		strXML += "<versionnumber>" + versionNumber.trim() + "</versionnumber>";

		strXML += "</request></mobile>";
		AndroidXmlParser xp = null;
		String msg = "";
		msg = HttpClient.PostXml(domain, strXML, Configuration.CLIENTINFO,
				"utf-8", Configuration.EXP_TIMEOUT);
		if (msg != null) {
			xp = new AndroidXmlParser(msg);
			try {
				ret = xp.valueOf("response", XmlResult.class);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return ret;
	}

	public static XmlResult sendCoordinate(String usrName, String lo, String la) {
		String path = Configuration.URL + Configuration.PROJECTNAME
				+ "/servlet/AccountServlet?format=userPos&userId=" + usrName
				+ "&longi=" + lo + "&lati=" + la;
		// ?format=userPos&userId=13911113230&longi=1&lati=2

		HttpURLConnection conn;

		XmlResult ret = new XmlResult();
		try {
			conn = (HttpURLConnection) new URL(path).openConnection();

			conn.setConnectTimeout(5000);
			conn.setRequestMethod("GET");

			String xmlStr = null;
			AndroidXmlParser xp = null;
			if (conn.getResponseCode() == 200) {
				InputStream is = conn.getInputStream();
				xmlStr = DataStream.RecvHttpData(is, "utf-8");
				if (xmlStr != null) {
					xp = new AndroidXmlParser(xmlStr);
					ret = xp.valueOf("response", XmlResult.class);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return ret;
	}

	/**
	 * 获取验找回密码证码
	 * 
	 * @param uid
	 * @param card
	 * @return
	 */
	public static XmlResult findValidationNumber(String uid, String flag,
			String card) {
		String domain = Configuration.URL + Configuration.PROJECTNAME
				+ "/servlet/Portal";
		XmlResult ret = new XmlResult();
		AndroidXmlParser xp = null;
		String strXML = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n";
		strXML += "<mobile><request>";
		if ("login".equals(flag)) {
			strXML += "<method>account-Resetloginpasswordsendauditnum</method>";
			strXML += "<useraccount>" + uid.trim() + "</useraccount>";
		} else {
			strXML += "<method>account-Resetpaypasswordsendauditnum</method>";
			strXML += "<useraccount>" + uid.trim() + "</useraccount>";
			strXML += "<card>" + card.trim() + "</card>";
		}
		strXML += "</request></mobile>";
		String msg = "";

		msg = HttpClient.PostXml(domain, strXML, Configuration.CLIENTINFO,
				"utf-8", Configuration.EXP_TIMEOUT);

		if (msg != null) {
			xp = new AndroidXmlParser(msg);
			try {
				ret = xp.valueOf("response", XmlResult.class);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		return ret;
	}

	/**
	 * 找回密码，将密码发至手机上
	 * 
	 * @param uid
	 * @param flag
	 * @param card
	 * @param validation
	 * @return
	 */
	public static XmlResult findSubmit(String uid, String flag, String card,
			String validation) {
		String domain = Configuration.URL + Configuration.PROJECTNAME
				+ "/servlet/Portal";
		XmlResult ret = new XmlResult();
		AndroidXmlParser xp = null;
		String strXML = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n";
		strXML += "<mobile><request>";
		strXML += "<method>account-Resetpassword</method>";
		strXML += "<type>" + flag.trim() + "</type>";
		strXML += "<useraccount>" + uid.trim() + "</useraccount>";
		strXML += "<card>" + card + "</card>";
		strXML += "<auditnum>" + validation + "</auditnum>";
		strXML += "</request></mobile>";
		String msg = "";

		msg = HttpClient.PostXml(domain, strXML, Configuration.CLIENTINFO,
				"utf-8", Configuration.EXP_TIMEOUT);

		if (msg != null) {
			xp = new AndroidXmlParser(msg);
			try {
				ret = xp.valueOf("response", XmlResult.class);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		return ret;
	}

	/**
	 * 获取验证码
	 * 
	 * @param uid
	 * @return
	 */
	public static XmlResult sendValidationNumber(String uid, String func_name) {
		String domain = Configuration.URL + Configuration.PROJECTNAME
				+ "/servlet/Portal";
		XmlResult ret = new XmlResult();
		AndroidXmlParser xp = null;
		String strXML = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n";
		strXML += "<mobile><request>";
		strXML += "<method>account-Sendauditnum</method>";
		strXML += "<useraccount>" + uid.trim() + "</useraccount>";
		strXML += "<funcname>" + func_name.trim() + "</funcname>";
		// ylj 2013-04-15 表示android客户端
		strXML += "<clientos>3</clientos>";
		strXML += "</request></mobile>";
		String msg = "";

		msg = HttpClient.PostXml(domain, strXML, Configuration.CLIENTINFO,
				"utf-8", Configuration.EXP_TIMEOUT);

		if (msg != null) {
			xp = new AndroidXmlParser(msg);
			try {
				ret = xp.valueOf("response", XmlResult.class);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		return ret;
	}

	/**
	 * 返回客户安全策略列表
	 * 
	 * @param uid
	 *            手机号码
	 * @return 客户的安全策略放到content里:01,02,03
	 */
	public static XmlResult getSecuritynum(String uid) {
		String domain = Configuration.URL + Configuration.PROJECTNAME
				+ "/servlet/Portal";
		XmlResult ret = new XmlResult();
		AndroidXmlParser xp = null;
		String strXML = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n";
		strXML += "<mobile><request>";
		strXML += "<method>account-Securitynum</method>";
		strXML += "<useraccount>" + uid.trim() + "</useraccount>";
		// ylj 2013-04-15 表示android客户端
		strXML += "<clientos>3</clientos>";
		strXML += "</request></mobile>";
		String msg = "";

		msg = HttpClient.PostXml(domain, strXML, Configuration.CLIENTINFO,
				"utf-8", Configuration.EXP_TIMEOUT);

		if (msg != null) {
			xp = new AndroidXmlParser(msg);
			try {
				ret = xp.valueOf("response", XmlResult.class);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		return ret;
	}

	/**
	 * 实现 2012-10-11 ylj
	 * 
	 * @param phoneNumber
	 *            String 手机号码
	 * @param verification
	 *            String 验证码
	 * @return
	 */
	public static Object test(String phoneNumber, String verification) {
		XmlResult ret = null;
		ret = new XmlResult();
		ret.setAction("ss");
		ret.setStatus("000000");
		ret.setMessage("提交成功！");
		return ret;
	}
}
